package Demo2;

//克隆 深拷贝

class Money implements Cloneable {
    public int m = 19;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable {
    public int age = 15;

    Money money = new Money();

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                '}';
    }


    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person)super.clone();
        tmp.money = (Money) this.money.clone();
        return tmp;
    }
}


public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {

        Person person1 = new Person();
        Person person2 = (Person) person1.clone();
        System.out.println(person1.money.m);
        System.out.println(person2.money.m);
        person2.money.m = 99;
        System.out.println("====================");
        System.out.println(person1.money.m);
        System.out.println(person2.money.m);
    }
}
